<template>
  <div class="container">
    <div class="app-container">
      <el-button type="primary"
                 class="add-btn"
                 @click="addPermission(0,1)">添加权限</el-button>
      <el-table :data="permissionList"
                row-key="id"
                default-expand-all>
        <el-table-column label="名称"
                         prop="name"
                         align="center"></el-table-column>
        <el-table-column label="标识"
                         prop="code"
                         align="center"></el-table-column>
        <el-table-column label="描述"
                         prop="description"
                         align="center"></el-table-column>
        <el-table-column label="操作"
                         align="center">
          <template v-slot="{row}">
            <el-row>
              <el-button v-if="row.type ===1"
                         type="text"
                         @click="addPermission(row.id,2)">添加</el-button>
              <el-button type="text"
                         @click="editPermission(row.id)">编辑</el-button>
              <el-popconfirm title="确定删除该权限吗"
                             @onConfirm="confirmDel(row.id)">
                <el-button type="text"
                           slot="reference">删除</el-button>
              </el-popconfirm>

            </el-row>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <el-dialog :visible.sync="addDialog"
               :title="`${showTitle}权限点`"
               @close="btnCancel">
      <el-form :model="permissionForm"
               :rules="permissionRules"
               ref="permissionForm">
        <el-form-item label="权限名称"
                      prop="name">
          <el-input v-model="permissionForm.name"></el-input>
        </el-form-item>
        <el-form-item label="权限标识"
                      prop="code">
          <el-input v-model="permissionForm.code"></el-input>
        </el-form-item>
        <el-form-item label="权限描述"
                      prop="description">
          <el-input v-model="permissionForm.description"></el-input>
        </el-form-item>
        <el-form-item label="开启"
                      prop="enVisible">
          <template>
            <el-switch v-model="permissionForm.enVisible"
                       :active-value="1"
                       :inactive-value="0"></el-switch>
          </template>
        </el-form-item>

        <el-form-item>
          <template>
            <el-row>
              <el-button type="primary"
                         @click="btnOK">确定</el-button>
              <el-button @click="btnCancel">取消</el-button>
            </el-row>
          </template>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { getPermissionList, addPermission, updatePermission, deletePermission, getPermissionDetail } from '@/api/permission'
import { transListToTree } from '@/utils'
export default {
  name: 'Permission',
  data () {
    return {
      permissionList: [],
      addDialog: false,
      permissionForm: {
        name: '',
        code: '',
        description: '',
        type: '',
        pid: '',
        enVisible: ''
      },
      permissionRules: {
        name: [
          { required: true, message: '请输入权限名称', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入权限标识', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入权限描述', trigger: 'blur' }
        ]
      },


    }
  },
  created () {
    this.getPermissionList()

  },
  computed: {
    showTitle () {
      return this.permissionForm.id ? '编辑' : '新增'
    }
  },
  methods: {
    async getPermissionList () {
      this.permissionList = transListToTree(await getPermissionList(), 0)
      console.log(this.permissionList);
    },
    addPermission (pid, type) {
      this.permissionForm.pid = pid
      this.permissionForm.type = type
      this.addDialog = true
    },
    btnOK () {
      this.$refs.permissionForm.validate(async isOK => {
        if (isOK) {
          if (this.permissionForm.id) {
            await updatePermission(this.permissionForm)
          } else {
            await addPermission(this.permissionForm)
          }
          // this.$message.success('新增成功')
          this.getPermissionList()
          this.btnCancel()
        }

      })
    },
    btnCancel () {
      this.permissionForm = {
        name: '', // 名称
        code: '', // 标识
        description: '', // 描述
        type: '', // 类型 该类型 不需要显示 因为点击添加的时候已经知道类型了
        pid: '', // 因为做的是树 需要知道添加到哪个节点下了
        enVisible: '0' // 开启
      }
      this.$refs.permissionForm.resetFields()
      this.addDialog = false
    },
    async confirmDel (id) {
      // console.log(id);
      await deletePermission(id)
      this.getPermissionList()
      this.$message.success('删除成功')
    },
    async editPermission (id) {
      this.permissionForm = await getPermissionDetail(id)
      this.addDialog = true
    }

  }
}
</script>
<style scoped>
.add-btn {
  margin: 10px;
}
</style>